# -*- coding: utf-8 -*-
# !/usr/bin/env python
import json
import random

from pydantic import BaseModel
from fastapi import APIRouter, Form, Body
from starlette.responses import HTMLResponse, JSONResponse
from fastapi.encoders import jsonable_encoder
from starlette.requests import Request
from starlette.templating import Jinja2Templates
from starlette.responses import FileResponse
from starlette.staticfiles import StaticFiles

router = APIRouter()
templates = Jinja2Templates(directory="./templates")
router.mount("/static", StaticFiles(directory="./static"), name="static")


class Item(BaseModel):
    title: str = None


@router.get('/')
def index(request: Request):
    with open("./config/init.json", 'r') as load_f:
        data = json.load(load_f)
    menu = data['menu']['analysis']
    return templates.TemplateResponse('./index.html', {'request': request, 'data': data, 'menu': menu})


@router.get('/sql')
def index(request: Request):
    with open("./config/init.json", 'r') as load_f:
        data = json.load(load_f)
    menu = data['menu']['analysis']
    return templates.TemplateResponse('./analysis/sql.html', {'request': request, 'data': data, 'menu': menu})


@router.get('/look')
def index(request: Request):
    with open("./config/init.json", 'r') as load_f:
        data = json.load(load_f)
    menu = data['menu']['analysis']
    return templates.TemplateResponse('./analysis/look.html', {'request': request, 'data': data, 'menu': menu})


@router.get('/query')
def index(request: Request):
    with open("./config/init.json", 'r') as load_f:
        data = json.load(load_f)
    menu = data['menu']['analysis']
    return templates.TemplateResponse('./analysis/query.html', {'request': request, 'data': data, 'menu': menu})


@router.post('/sql/result')
def aaa(item: Item):
    a = item.title
    return f"<h1>{a}</h1>"


@router.post('/query/result')
def aaa(item: Item):
    i = random.randint(1, 5)
    return f"""<img src='/static/img/图片{i}.jpg' width='60%' hight='60%'>"""


@router.post('/query/result/id')
def aaa(item: Item):
    data = {'data': [50, 65, 23, 35, 23, 66]}
    data = jsonable_encoder(data)
    print(data)
    return JSONResponse(content=data, media_type="application/json")
